package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import application.EffortMultiplyer;
import application.Project;

public class CalcEffortListener implements ActionListener{

	private GUI gui;
	
	public CalcEffortListener(GUI gui){
		this.gui = gui;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Project p = new Project();
		Double[][] data = gui.aesp.getCharsData();
		Double value;
		int index = 0;
		for (int i = 0; i < gui.nbrOfComboboxes; i++) {
			value = data[i][EffortMultiplyer.getVectorPlace(
					gui.characteristics[i])];
			//System.out.println(value);
			p.addCharacteristic(gui.charsNames[index], value);
			index++;
		}
		for (int i = 0; i < gui.nbrOfTextFields; i++) {
			p.addCharacteristic(gui.charsNames[index], Double.parseDouble(gui.characteristics[index]));
		}
		gui.aesp.setThreshold(gui.thresholdAsWholePercentage());
		Double result;
		try {
			result = gui.aesp.estimateEffort(p, gui.getEstimationUnit(gui.estUnit));
		
		
		String fresult = String.format("%.3f%n", result);
		if (gui.estUnit == 1) {
			gui.effort.setText("Effort: " + fresult + " Person hours" );
		} else if (gui.estUnit == 2) {
			gui.effort.setText("Effort: " + fresult + " Person days" );
		} else if (gui.estUnit == 3) {
			gui.effort.setText("Effort: " + fresult + " Person months" );
		} else {
			gui.effort.setText("Effort: " + fresult + " Person years" );
		}
		
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			gui.effort.setText("Effort: ");
			JOptionPane.showMessageDialog(gui,
					"The new project is to different from the old projects", 
					"Message", JOptionPane.INFORMATION_MESSAGE);
		}
		
		
		gui.repaint();		
	}

}
